/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2021并保留所有权利。
 * Copyright 2021, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.www.cnblogs.com.junma.com.zte.zoo.keeper.configuration;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.CuratorWatcher;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;

public class Server{
    public static void main(String[] args) {
        final String serverName="app2";
        final CuratorFramework client = CuratorFrameworkFactory.newClient("172.16.7.141:2181", new ExponentialBackoffRetry(10, 3));
        client.start();
        try {
            client.getData().usingWatcher(new CuratorWatcher() {
                @Override
                public void process(WatchedEvent watchedEvent) throws Exception {
                    if (Watcher.Event.EventType.NodeDataChanged == watchedEvent.getType()) {
                        String data = new String(client.getData().forPath(watchedEvent.getPath()));
                        System.out.println(serverName + " recv data:" + GsonUtils.fromJson(data, ConfigBean.class));
                    }
                }
            }).forPath("/conf");
        } catch (Exception e) {
            e.printStackTrace();
        }
        while (true){}
    }

}